from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from mptt.models import MPTTModel
from d115.utils.mathutils import mark_down
from d115.fields import AutoSlugField
from d115.mixins import *

class ProductCategory(MPTTModel):
    name = models.CharField(max_length=255)
    language = models.CharField(max_length=12, choices=settings.LANGUAGES, verbose_name=_("Language"), default=settings.LANGUAGE_CODE)
    keywords = models.CharField(max_length=1024, blank=True, null=True, verbose_name=_("Keywords"))
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
    slug = AutoSlugField(max_length=255, verbose_name=_("Slug"), populate_from=("name", ), editable=True)
    icon = models.ImageField(max_length=255, upload_to="icons/", blank=True, null=True, verbose_name=_("Icon"))
    position = models.PositiveIntegerField(default=0, verbose_name=_("Position"))

    def __unicode__(self):
        if self.parent:
            return "%s >> %s" % (self.parent.__unicode__(), self.name)
        return self.name

    class Meta:
        verbose_name=_("product category")
        verbose_name_plural=_("product categories")
        #ordering = ('tree_id', 'lft', )
        ordering = ['position', 'name',]
    
    @models.permalink
    def get_absolute_url(self):
        return ('product-cat-view', (), {'slug':self.slug,})

class Product(NomenclatureMixin, AuditMixin):
    category = models.ForeignKey(ProductCategory, verbose_name=_("Category"), related_name="products")
    language = models.CharField(max_length=12, choices=settings.LANGUAGES, verbose_name=_("Language"), default=settings.LANGUAGE_CODE)
    headline = models.TextField(blank=True, null=True, verbose_name=_("Headline"))
    description = models.TextField(blank=True, null=True, verbose_name=_("Description"))
    action = models.TextField(blank=True, null=True, verbose_name=_("Action box"))
    price = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
    discount = models.PositiveIntegerField(blank=True, null=True, verbose_name=_("Discount %"))
    icon = models.ImageField(max_length=255, upload_to="icons/", blank=True, null=True, verbose_name=_("Icon"))
    active = models.BooleanField(default=True, verbose_name=_("Is active?"))
    slug = AutoSlugField(max_length=255, verbose_name=_("Slug"), populate_from=("name", ), editable=True)

    class Meta(NomenclatureMixin.Meta):
        verbose_name = _("product")
        verbose_name_plural = _("products")

    @models.permalink
    def get_absolute_url(self):
        return ('show-product', (), {'slug':self.slug,})

    def final_price(self):
        return mark_down(self.discount or 0, self.price or 0)

    def savings(self):
        return (self.price or 0) - self.final_price()

class ProductOrder(AuditMixin):
    product = models.ForeignKey(Product, blank=True, null=True, verbose_name=_("Product"))
    user = models.ForeignKey(User, blank=True, null=True, verbose_name=_("User"), related_name="orders")
    payment_method = models.CharField(max_length=255, choices=(("paypal", _("PayPal")), ("epay", _("E-Pay")), ("sms", _("SMS")), ("other", _("Other"))), verbose_name=_("Payment method"))
    amount = models.DecimalField(decimal_places=2, max_digits=8, default=0, verbose_name=_("Price"))
    status = models.CharField(max_length=255, choices=(("pending", _("Pending")), ("processing", _("Processing")), ("completed", _("Completed")), ("canceled", _("Canceled")), ("expired", _("Expired")), ("other", _("Other"))), default="pending", verbose_name=_("Status"))
    comments = models.TextField(default="", verbose_name=_("Comments"))
    class Meta(AuditMixin.Meta):
        verbose_name = _("product order")
        verbose_name_plural = _("product orders")
    def __unicode__(self):
        return u"%s / %s" % (self.product, self.user)

